<?php

/*
 * This will be the Create Department page. 
 * 
 * Created by: Peter Agno Jr.
 * Date created: November 22, 2011
 */

    include("siteTop.php"); 
    include('includes/siteConfig.php'); 

    // connect to database
    $connect = mysql_connect($hostName,$rootName,$dBasePassword) or die ('Unable to connect!');
    mysql_select_db($dBaseName) or die ('Unable to select database!');
?>

<!-- HEADER START -->
    <!--JAVASCRIPT SOURCES-->
    <script src="js/jquery-1.6.2.min.js"></script>
    
    <!--ACTUAL JS SCRIPTS AND JQUERIES-->
    <script type="text/javascript">
        jQuery(document).ready(function(){ 
            // CHANGE FIRST THE CSS OF THE NAV BAR
            jQuery("#adminTools-nav").addClass("highlightMenuBar");
            jQuery("#orgSetup-nav").addClass("highlightMenuBar");
            jQuery("#deptSetup-nav").addClass("highlightMenuBar");
            
            jQuery("#deptId").focus();
            
            // Start - Perform click when user pressed enter
            jQuery(".inputbox").keypress(function(e){
                if(e.keyCode == 13) {
                    jQuery("#createDeptButton").click();
                }
            }); // End - Perform submit when user pressed enter
            
            // Start - Go back to admDepartmentSetup.php
            jQuery('#admDeptStpBtn').live("click",function() {
                window.location = 'admDepartmentSetup.php';
            }); // End - Go back to admPositionSetup.php
            
            // Start - create department
            jQuery('#createDeptButton').live("click",function() {
                if ( jQuery("#deptId").val() == "" || jQuery("#deptName").val() == "" ) {
                    // Show error message if the user clicked the create button
                    // but haven't inputted all of the neccessary information.
                    alert("Please input the required information.");
                }
                else {
                    // Start - Ajax the creating of department
                    jQuery.ajax({
                        type : 'POST',
                        url : 'ajaxRequests/admCreateDept.php',
                        dataType : 'json',
                        async : false,
                        data: {
                            deptId : jQuery("#deptId").val(),
                            deptName : jQuery("#deptName").val()
                        },
                        success : function(data){
                            // If, successful creation of department
                            if (data) {
                                if ( data[0].flag == "success" ) {
                                    jQuery("#deptSuccessMessage").val(data[0].msg);
                                    jQuery("#deptStpMsgForm").submit();
                                }
                                else {
                                    alert(data[0].msg);
                                }
                            } // End if, successful creation of department
                        },
                        error : function() {
                            alert("Error : Creating the department was not successful.");
                        }
                    }); // End - Ajax the creating of department
                } // End else
                
            }); // End - create department
            
        }); // End - jQuery document ready
    </script>
<!-- HEAD END -->

<div id="content">
<h1>Create Department</h1>
    <div class="contentbox">       
        <?php
        // Check permission
        $thePermissions = array('DEPTSTP');
        if ( checkPermission( $thePermissions, $userId ) == true ) {
        ?>
            <span class="screenlet" >Department Information</span>
            <table>
                <tr>
                    <td>Department ID : </td>
                    <td>
                        <input type="text" value="" name="deptId" id="deptId" class="inputbox"/>
                    </td>
                </tr>
                <tr>
                    <td>Department Name : </td>
                    <td>
                        <input type="text" value="" name="deptName" id="deptName" class="inputbox"/>
                    </td>
                </tr>
            </table>
            
            <input type="Submit" name="createDeptButton" id="createDeptButton" value="Create" class="siteGreenButton" />
            
            <!-- Display back button -->
            <input type="Submit" name="admDeptStpBtn" id="admDeptStpBtn" value="Cancel" class="siteRedButton" />
            
            <!-- Hidden form to be used when the creation of department was successful -->
            <form id="deptStpMsgForm" name="deptStpMsgForm" action="admDepartmentSetup.php" method="POST">
                <input type="hidden" name="deptSuccessMessage" id="deptSuccessMessage" value=""/>
            </form>
        <?php
        }
        else {
            echo "<div class='noPermissionMsgBox noPermissionMsg' style='border: 2px solid red'><div id='noPermissionMsgs'>You have no permission to view this page.</div></div>";
        }
        // End checking permission
        ?>
    </div> <!-- contentbox end div -->
    <div style="position:relative; visibility: hidden">end</div>    <!-- extra space for contentbox -->
</div> <!-- content end div -->
<?php
    include ("siteBottom.php"); 
?>

